package com.whh.tq;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

public class TimeTableActivity extends TabActivity implements OnTouchListener, OnTabChangeListener,OnGestureListener {

	private GestureDetector gestureDetector;
	private SlideTabHost tabHost;
	private TabWidget tabWidget;
	private int currentTabIndex = 0;
	
	private static final int FLING_DISTANCE = 120;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_timetable);
		init();
	}
	
	private void init(){
		gestureDetector = new GestureDetector(this);
		tabHost = (SlideTabHost)findViewById(android.R.id.tabhost);
		tabWidget= (TabWidget)findViewById(android.R.id.tabs);
		
		tabHost.setOnTabChangedListener(this);

		setIndicator(R.string.lable_tab_trainCodeQuery, 0,  new Intent(this,TrainCodeInputTabActivity.class));
		setIndicator(R.string.lable_tab_fromToQuery, 1, new Intent(this,FromToInputTabActivity.class));
		setIndicator(R.string.lable_tab_stationQuery, 2, new Intent(this,StationInputTabActivity.class));
	}
	
	private void setIndicator(int text, int tabId, Intent intent) {
		View view = LayoutInflater.from(this.tabHost.getContext()).inflate(R.layout.tab_timetable, null);
		TextView textView = (TextView)view.findViewById(R.id.tab_text);
		textView.setText(text);
		String str = String.valueOf(tabId);
		TabHost.TabSpec localTabSpec = tabHost.newTabSpec(str).setIndicator(view).setContent(intent);
		tabHost.addTab(localTabSpec);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		if (gestureDetector.onTouchEvent(event)) {
			event.setAction(MotionEvent.ACTION_CANCEL);
		}
		return super.dispatchTouchEvent(event);
	}
	
	@Override
	public void onTabChanged(String tabId) {
		//tabId值为要切换到的tab页的索引位置
		int tabID = Integer.valueOf(tabId);
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			TextView tv = (TextView)tabWidget.getChildAt(Integer.valueOf(i)).findViewById(R.id.tab_text);
			if (i == tabID) {
				tv.setBackgroundResource(R.drawable.tab_select_top_arrow);
			} else {
				tv.setBackgroundResource(0);
			}
		}		
	}

	@Override
	public boolean onTouch(View view, MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}
	
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (e1.getX() - e2.getX() <= (-FLING_DISTANCE)) {//从左向右滑动
			currentTabIndex = tabHost.getCurrentTab() - 1;
			if (currentTabIndex < 0) {//超过最左边tab时，变为最右边tab
				currentTabIndex = tabHost.getTabCount() - 1;
			}
		} else if (e1.getX() - e2.getX() >= FLING_DISTANCE) {//从右向左滑动
			currentTabIndex = tabHost.getCurrentTab() + 1;
			if (currentTabIndex >= tabHost.getTabCount()) {//超过最右边tab时，变为最左边tab
				currentTabIndex = 0;
			}
		}
		tabHost.setCurrentTab(currentTabIndex);
		return false;
	}

	@Override
	public boolean onDown(MotionEvent arg0) {
		return false;
	}
	
	@Override
	public void onLongPress(MotionEvent arg0) {
		
	}

	@Override
	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,float arg3) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent arg0) {
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent arg0) {
		return false;
	}

}
